<template>
  <el-container>
    <el-header>
      <div>
        <img src="../assets/dao.jpg" alt="头像" />
        <span>管理平台</span>
      </div>
      <el-button type="info" @click="exit">退出</el-button>
    </el-header>

    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse? '64px' : '200px'">
        <div class="btn-toggle" @click="toggleCollapse">|||</div>

        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409bff"
          unique-opened
          router
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模版区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="menuIconlist[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+it.path"
              v-for="it in item.children"
              :key="it.id"
              @click="saveNavState('/'+it.path)"
            >
              <!-- 二级菜单模版区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ it.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      menuIconlist: {
        "125": "el-icon-user-solid",
        "103": "el-icon-aim",
        "101": "el-icon-s-goods",
        "102": "el-icon-s-order",
        "145": "el-icon-s-data"
      },
      isCollapse: false,
      activePath: ""
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath');
  },
  methods: {
    exit() {
      //退出当前登录状态，清空token，返回登录界面
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) {
        alert("获取权限错误！");
      }
      this.menulist = res.data;
    },
    toggleCollapse() {
      // 点击按钮侧边栏折叠
      this.isCollapse = !this.isCollapse;
    },
    saveNavState(activePath) {
      // 保持链接的激活状态
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath
    }
  }
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  overflow: hidden;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding: 0;
  color: #fff;
  font-size: 20px;
  align-items: center;

  div {
    display: flex;
    align-items: center;
    img {
      height: 60px;
    }
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  transition: all 0.5s cubic-bezier(0, 0.86, 1, 1.59);

  .btn-toggle {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }

  .el-menu {
    border: none;
  }
}

.el-main {
  background-color: #eaedf1;
}
</style>
